package com.cml.getway.client.core.autoconfigure;

import com.cml.getway.client.support.dubbo.Dubbo27ClientRegisteryManager;
import com.cml.getway.client.support.springmvc.SpringMVCClientRegisteryManager;
import org.apache.dubbo.config.spring.ServiceBean;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Servlet;

/**
 * 姓名：陈茂林
 * 时间：2024/1/19
 * 描述：
 */
@Configuration
@EnableConfigurationProperties(GetwayProperties.class)
@ConditionalOnProperty(prefix = GetwayProperties.GETWAY_PREFIX, name = {"registryAddress", "namespace"})
public class GetwayClientAutoConfiguration {
    @Autowired
    private GetwayProperties getwayProperties;

    @Bean
    @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
    @ConditionalOnMissingBean(SpringMVCClientRegisteryManager.class)
    public SpringMVCClientRegisteryManager springMVCClientRegisteryManager() throws Exception {
        return new SpringMVCClientRegisteryManager(getwayProperties);
    }

    @Bean
    @ConditionalOnClass({ServiceBean.class})
    @ConditionalOnMissingBean(Dubbo27ClientRegisteryManager.class)
    public Dubbo27ClientRegisteryManager dubbo27ClientRegisteryManager() throws Exception {
        return new Dubbo27ClientRegisteryManager(getwayProperties);
    }
}
